<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="goodsName">
        <el-input
          v-model="queryParams.goodsName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['biz:goodsInfoWarning:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品编号" align="center" prop="goodsId" />
      <el-table-column label="商品名称" align="center" prop="goodsName" />
      <el-table-column label="商品图片" align="center" prop="goodsImg">
         <template slot-scope="scope">
          <el-image v-if="scope.row.goodsImg!=null" style="width:70px; height:70px" :src="scope.row.goodsImg" :preview-src-list="getImgs(scope.row.goodsImg)"></el-image>
         </template>
      </el-table-column>
      <el-table-column label="轮播图" align="center" prop="carouselImg">
        <template slot-scope="scope">
          <el-image v-if="scope.row.carouselImg!=null" style="width:70px; height:70px" :src="getImg(scope.row.carouselImg)" :preview-src-list="getImgs(scope.row.carouselImg)"></el-image>
         </template>
      </el-table-column>
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="规格" align="center" prop="specs" />
      <el-table-column label="总库存" align="center" prop="stockTotal" />
      <el-table-column label="已售库存" align="center" prop="soldStock" />
      <el-table-column label="剩余库存" align="center" prop="stockNum" />
      <el-table-column label="消耗百分比" align="center" prop="percentSpent" />
      <el-table-column label="供应商" align="center" prop="tenantName" />
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品信息对话框 -->
    <!-- <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="goodsName">
          <el-input v-model.trim="form.goodsName" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品图片" prop="goodsImg">
          <imageUpload v-model="form.goodsImg" :limit="1"/>
        </el-form-item>
        <el-form-item label="轮播图" prop="carouselImg">
           <imageUpload v-model="form.carouselImg" :limit="5"/>
        </el-form-item>
        <el-form-item label="商品类型" prop="type">
            <el-radio v-model="form.type" :label="0">实物商品</el-radio>
            <el-radio v-model="form.type" :label="1">虚拟商品<span style="font-size:10px; color:red">  (*虚拟商品库存设置请去导入库存excel文件)</span></el-radio>
        </el-form-item>
        <el-form-item label="价格" prop="price">
           <el-input v-model.number="form.price" type="number" />
        </el-form-item>
        <el-form-item label="规格" prop="specs">
          <el-input v-model.trim="form.specs" placeholder="请输入规格" />
        </el-form-item>
        <el-form-item label="详细信息" prop="details">
          <editor v-model="form.details" :min-height="192" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="总库存" prop="stockTotal" v-show="form.type==0">
          <el-input type="number" v-model="form.stockTotal" placeholder="请输入总库存" />
        </el-form-item>
        <el-form-item label="供应商" prop="tenantId">
          <el-select v-model="form.tenantId" placeholder="请选择供应商" filterable clearable size="small">
            <el-option
              v-for="item in tenantList"
              :key="item.tenantId"
              :label="item.name"
              :value="item.tenantId">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog> -->
  </div>
</template>

<script>
import { inventoryWarning, inventoryWarningExport } from "@/api/biz/goodsInfo/goodsInfo";
export default {
  name: "inventoryWarning",
  data() {
    return {
      baseUrl: "",
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品信息表格数据
      goodsInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 租户列表
      tenantList: [],
      isPla: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        goodsName: null,
        type: 1,
        goodsIds:[]
      },
      // 表单参数
      form: {
        type: 1
      },
      // 表单校验
      rules: {
        
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商品信息列表 */
    getList() {
      this.loading = true;
      inventoryWarning(this.queryParams).then(response => {
        this.goodsInfoList = response.rows;
        this.total = Number(response.total);
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        goodsId: null,
        goodsName: null,
        goodsImg: null,
        carouselImg: null,
        specs: null,
        details: null,
        stockTotal: 0,
        resStock: null,
        soldStock: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        delFlag: null,
        tenantId: null,
        price: null,
        type: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.goodsId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.goodsIds = this.ids
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出库存预警数据项？').then(() => {
        this.exportLoading = true;
        return inventoryWarningExport(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    getImg(value){
      let arr=value.split(",");
      return arr[0];
    },
    getImgs(urls){
      let arr=urls.split(",");
      for(let i=0;i<arr.length;i++){
        arr[i]= arr[i];
      }
      return arr;
    }
  }
};
</script>
